<?php

namespace App\Admin\Controllers\User;

use App\Admin\Actions\Extensions\User\UserInviteTmpCodeBatchButton;
use App\Admin\Actions\Extensions\User\UserInviteTmpCodeBgButton;
use App\Admin\Actions\Grid\User\UserInviteTmpCodeBgDownBatchAction;
use App\Admin\Actions\Grid\User\UserInviteTmpCodeDownBatchAction;
use App\Admin\Repositories\User\WxUserInviteTempCode;
use App\Models\WxTenant;
use App\Wen\Utils\Settings;
use App\Wen\Utils\Utils;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxUserInviteTempCodeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxUserInviteTempCode(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('type')->using([1=>'传单', 2=>'桌牌'])->label([1=>'#28a745', 2=>'#007bff']);
            $grid->column('code')->image('', 50, 50);
            $grid->column('path');
            $grid->column('owers', '头像')->display(function ($v) {
                if(_empty_($this->ower)){
                    return '';
                }
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if(_empty_($this->code)){
                    $scene = 'from_user=itc'.$this->id;
                    if($this->tenant_id !== null && $this->tenant_id >= 0){
                        if(!_empty_($scene)){
                            $scene .= '&';
                        }
                        $scene .= 'tenant='.$this->tenant_id;
                    }
                    if(!_empty_($this->scene) && strlen($scene) < 32){
                        if(!_empty_($scene)){
                            $scene .= '&';
                        }
                        $scene .= $this->scene;
                    }
                    $code = Utils::getMiniCode(_empty_default_($this->path, '/pages/tabbar/index/index') , $scene, 0, 430, false, null, true);
                    \App\Models\User\WxUserInviteTempCode::where('id', $this->id)->update(['code' => $code]);
                }

                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '';
            })->width('100px');
            $grid->column('shop_id');
            $grid->column('club_id');
            $grid->column('circle_id');
            $grid->column('tag_id');
            $grid->column('voter_id');
            $grid->column('tenant_id');
            $grid->column('status')->using([0=>'待绑定', 1=>'已绑定', 2=>'已解绑'])->label([0=>'#5ac725', 1=>'primary', 2=>'gray']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->tools([
                new UserInviteTmpCodeBatchButton(),
                new UserInviteTmpCodeBgButton()
            ]);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->batchActions(function ($batch) {
                $batch->add(new UserInviteTmpCodeDownBatchAction());
                $batch->add(new UserInviteTmpCodeBgDownBatchAction());
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxUserInviteTempCode(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('code');
            $show->field('path');
            $show->field('scene');
            $show->field('with_target_type');
            $show->field('with_target_id');
            $show->field('user_id');
            $show->field('shop_id');
            $show->field('club_id');
            $show->field('circle_id');
            $show->field('tag_id');
            $show->field('voter_id');
            $show->field('tenant_id');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxUserInviteTempCode(), function (Form $form) {
            $form->display('id');
            $form->radio('type')->options([1=>'传单', 2=>'桌牌'])->default(1);
            $form->text('path')->default('/pages/tabbar/index/index');
            $form->text('user_id');
//            $form->text('shop_id');
//            $form->text('club_id');
//            $form->text('circle_id');
//            $form->text('tag_id');
//            $form->text('voter_id');
            $form->select('tenant_id')->options(WxTenant::orderBy('order', 'desc')->orderBy('tenant_id')->pluck('name', 'tenant_id'));
//            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
